<template>
  <div id="home">
    <el-container class="homeContainer">
      <el-header>
        <div>
          <span>电商后台管理系统</span>
        </div>
        <el-button type="info" @click="logout">退出</el-button>
      </el-header>
      <el-container>
        <el-aside :width="isFlod?'64px':'200px'">
          <div class="toggleBtn" @click="toggleMenu"> >>></div>
          <el-menu background-color="#545c64" text-color="#fff" :collapse-transition="false" 
          :collapse="isFlod" active-text-color="#409eff" :unique-opened="true" :router="true"
          :default-active="activePath">
            <el-submenu :index="item.id+''" v-for="item in menulist" :key="item.id" >
              <template slot="title">
                <i :class="iconsObj[item.id]"></i>
                <span>{{item.authName}}</span>
              </template>
            <el-menu-item :index="'/'+subitem.path+''" v-for="subitem in item.children" :key="subitem.id">
                <template slot="title" @click="savaNative('/'+subitem.path)">
                <i class="el-icon-menu"></i>
                <span>{{subitem.authName}}</span>
              </template>
            </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>
<script>
export default {
  data () {
    return {
      menulist: [],
      iconsObj: {
        '125':'iconfont icon-user',
        '103':'iconfont icon-tijikongjian',
        '101':'iconfont icon-shangpin',
        '102':'iconfont icon-danju',
        '145':'iconfont icon-baobiao'
      },
      isFlod: false,
      activePath: ''
    }
  },
  methods: {
    logout: function () {
      window.sessionStorage.clear()
      this.$router.push('/')
    },
    async getMenus () {
      const { data: res } = await this.$http.get('menus')
      if(res.meta.status!=200) return this.$message.error(res.meta.msg)
      this.menulist=res.data
    },
    toggleMenu(){
      this.isFlod=!this.isFlod
    },
    savaNative: function(path){
      console.log(path)
      window.sessionStorage.setItem('activePath',path)
      this.activePath=path
    }
  },
  created() {
    this.getMenus()
    this.activePath=window.sessionStorage.getItem('activePath')
  }
}
</script>
<style scoped>
#home {
  height: 100%;
}
.homeContainer {
  height: 100%;
}
.el-header,
.el-footer {
  background-color: #b3c0d1;
  color: #333;
  height: 60px !important;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #d3dce6;
  color: #333;
  text-align: center;
  line-height: 200px;
}
.el-main {
  background-color: #ffffff;
}
.iconfont{
  margin-right: 10px;
}
.el-menu {
    border-right: none;
  }
.toggleBtn {
  background-color: #4b5f75;
  font-size: 10px;
  line-height: 24px;
  text-align: center;
  color: #ffffff;
  cursor: pointer;
}
</style>>
